package com.bds.wm.service;

import com.bds.wm.model.procmodel.*;
import com.bds.wm.model.view.CategorySubCategoryBrand;
import com.google.common.collect.Lists;
import org.apache.log4j.Logger;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.sql.Sql;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by DongChenchen on 2018/6/25 0025
 */
@Service
public class ShopViewService {
    private Logger LOGGER = Logger.getLogger(ShopViewService.class);

    @Resource
    private NutDao dao;

    /**
     * 品牌品类下拉框
     * @return map<品类，List<品牌>>
     */
    public Map<String,List<String>> getAllSubCategory(){
        Map<String,List<String>> subCateBrands=new HashMap<>();
        List<CategorySubCategoryBrand> query = dao.query(CategorySubCategoryBrand.class, null);
        query.forEach(brand->{
            String subCate=brand.getSubCategory();
            List<String> brands =null;
            if(subCateBrands.containsKey(subCate)){
                brands = subCateBrands.get(subCate);

            }else{
                brands=new ArrayList<>();
            }
            brands.add(brand.getBrand());
            subCateBrands.put(subCate,brands);
        });
        return subCateBrands;
    }
    public List<StoreNames> getStoreNameList(String start_month,
                                             String end_month, String brandName, String subcat_name){
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreNameList @StartMonth ,@EndMonth ,@SubCategoryName ,@BrandName ");
            sql.setCallback(Sqls.callback.entities());
            sql.setEntity(dao.getEntity(StoreNames.class));
            sql.params().set("StartMonth", start_month); //设置入参
            sql.params().set("EndMonth", end_month); //设置入参
            sql.params().set("SubCategoryName", subcat_name); //设置入参
            sql.params().set("BrandName", brandName); //设置入参
        dao.execute(sql);
        return sql.getList(StoreNames.class);
    }

    public List<StoreOverviewResultbyProduct> getStoreOverviewResultbyProduct(String start_month,
                                                                        String end_month, String brandName, String restaurantName){
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreOverviewResultbyProduct @StartMonth ,@EndMonth ,@BrandName ,@RestaurantName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreOverviewResultbyProduct.class));
        sql.params().set("StartMonth", start_month); //设置入参
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("RestaurantName", restaurantName); //设置入参
        sql.params().set("BrandName", brandName); //设置入参
        dao.execute(sql);
        return sql.getList(StoreOverviewResultbyProduct.class);
    }



    public StoreOverviewResult getStoreOverviewResult(String start_month, String end_month, String brandName, String restaurant) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreOverviewResult @StartMonth,@EndMonth,@BrandName,@Restaurant");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreOverviewResult.class));
        sql.params().set("StartMonth", start_month); //设置入参
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName", brandName); //设置入参
        sql.params().set("Restaurant", restaurant); //设置入参
        dao.execute(sql);
        return sql.getObject(StoreOverviewResult.class);
    }

    public StoreOverviewResultRate getStoreOverviewResultRate(int start_month, int end_month, String brand_name, String restaurant) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreOverviewResultRate @StartMonth,@EndMonth,@BrandName,@Restaurant");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreOverviewResultRate.class));
        sql.params().set("StartMonth", start_month); //设置入参
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName", brand_name); //设置入参
        sql.params().set("Restaurant", restaurant); //设置入参
        dao.execute(sql);
        return sql.getObject(StoreOverviewResultRate.class);
    }

    public List<StoreKeyWord> getStoreKeyWord(String end_month, String storeName,
                                              String brandName, String start_month) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreKeyWordE " +
                "@StartMonth,@EndMonth,@BrandName,@StoreName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreKeyWord.class));
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName",brandName ); //设置入参
        sql.params().set("StartMonth",start_month ); //设置入参
        sql.params().set("StoreName",storeName ); //设置入参
        dao.execute(sql);
        List<StoreKeyWord> words1=sql.getList(StoreKeyWord.class);
        //创建SQL对象,用于调用存储过程
        Sql sql2 = Sqls.fetchEntity("exec Report.getStoreKeyWordP " +
                "@StartMonth,@EndMonth,@BrandName,@StoreName");
        sql2.setCallback(Sqls.callback.entities());
        sql2.setEntity(dao.getEntity(StoreKeyWord.class));
        sql2.params().set("EndMonth", end_month); //设置入参
        sql2.params().set("BrandName",brandName ); //设置入参
        sql2.params().set("StartMonth",start_month ); //设置入参
        sql2.params().set("StoreName",storeName ); //设置入参
        dao.execute(sql2);
        List<StoreKeyWord> words2=sql2.getList(StoreKeyWord.class);
        List<StoreKeyWord> result = Lists.newArrayList(words2);
        words1.forEach(words->{
            words.setEmotion(1);
            result.add(words);
        });
        return result;
    }


    public List<StoreKeyWord> getStoreKeyWord2(String end_month, String storeName,
                                              String brandName, String start_month) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreKeyWord " +
                "@StartMonth,@EndMonth,@BrandName,@StoreName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreKeyWord.class));
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName",brandName ); //设置入参
        sql.params().set("StartMonth",start_month ); //设置入参
        sql.params().set("StoreName",storeName ); //设置入参
        dao.execute(sql);
        return sql.getList(StoreKeyWord.class);
    }



    public StoreBrand getStoreBrandList(String end_month,String restaurantID,String start_month) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreBrandList " +
                "@StartMonth,@EndMonth,@RestaurantID");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreBrand.class));
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("StartMonth",start_month ); //设置入参
        sql.params().set("RestaurantID",restaurantID ); //设置入参
        dao.execute(sql);
        return sql.getObject(StoreBrand.class);
    }

    public List<StoreMonthlyRatingTrend> getStoreMonthlyRatingTrend(int end_month, String brand_name, String restaurant) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreMonthlyRatingTrend @EndMonth,@BrandName,@Restaurant");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreMonthlyRatingTrend.class));
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName", brand_name); //设置入参
        sql.params().set("Restaurant", restaurant); //设置入参
        dao.execute(sql);
        return sql.getList(StoreMonthlyRatingTrend.class);
    }
    public List<ShopTrend> getStoreMonthlyTrend(int end_month, String brand_name, String restaurant) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreMonthlyTrend @EndMonth,@BrandName,@RestaurantName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(ShopTrend.class));
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName", brand_name); //设置入参
        sql.params().set("RestaurantName", restaurant); //设置入参
        dao.execute(sql);
        return sql.getList(ShopTrend.class);
    }
    public List<StoreOverviewResultComp> getcomstoreinfo(String start_month, String end_month,  String restaurant){
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreOverviewResultComp @StartMonth,@EndMonth,@RestaurantName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreOverviewResultComp.class));
        sql.params().set("StartMonth", start_month); //设置入参
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("RestaurantName", restaurant); //设置入参
        dao.execute(sql);
        return sql.getList(StoreOverviewResultComp.class);
    }
    public List<StoreOverviewResultRateComp> getcomstoreyoyinfo(String start_month, String end_month, String brand_name, String restaurant){
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getStoreOverviewResultRateComp @StartMonth,@EndMonth,@BrandName,@RestaurantName");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(StoreOverviewResultRateComp.class));
        sql.params().set("StartMonth", start_month); //设置入参
        sql.params().set("EndMonth", end_month); //设置入参
        sql.params().set("BrandName", brand_name); //设置入参
        sql.params().set("RestaurantName", restaurant); //设置入参
        dao.execute(sql);
        return sql.getList(StoreOverviewResultRateComp.class);
    }
    public List<CompetitorShops> getCompetitoronMap(String storeName,String startmonth ,String endMonth) {
        //创建SQL对象,用于调用存储过程
        Sql sql = Sqls.fetchEntity("exec Report.getCompetitoronMap @StoreName,@Startmonth,@EndMonth");
        sql.setCallback(Sqls.callback.entities());
        sql.setEntity(dao.getEntity(CompetitorShops.class));
        sql.params().set("StoreName", storeName); //设置入参
        sql.params().set("Startmonth", Integer.parseInt(startmonth)); //设置入参
        sql.params().set("EndMonth", Integer.parseInt(endMonth)); //设置入参
        dao.execute(sql);
        return sql.getList(CompetitorShops.class);
    }


}
